from lxml import etree
from properties import Properties
from sendEmail import SkypeSendEmail
import time
import os	

def syncByFile(file):
	xslt_doc = etree.parse(p['xslt'])
	transform = etree.XSLT(xslt_doc)

	filename=p['rootdir']+'/'+file
	print filename
	doc = etree.parse(filename)
	result_tree = transform(doc)
	html = str(result_tree)
	#print html

	roottag=doc.getroot()
	
	dialogPartner = str(roottag.xpath("/IMHISTORY/EVENT[1]/CONTACT//text()")[:1])
	dateTime = str(roottag.xpath("/IMHISTORY/EVENT[1]/DATE//text()")[:1])+' '+ str(roottag.xpath("/IMHISTORY/EVENT[1]/TIME//text()")[:1])
	#username=str(roottag.xpath("/IMHISTORY/EVENT/FROM[1]//text()")[:1])
	username=dialogPartner
	print dialogPartner+' ' +dateTime+ ' ' +username
	
	#print contact
	
	flg=1	
	skypeEmail=SkypeSendEmail()
	#skypeEmail.send_email(dialogPartner, dateTime, html , username)
	while flg==1:
		try:
			print 'send email'
			print skypeEmail.send_email(dialogPartner, dateTime, html , username)
			flg=0
		except:
			time.sleep(5)
			
	#time.sleep(10)

p = Properties()
p.load(open('skype.properties'))

for subdir, dirs, files in os.walk(p['rootdir']):
	for file in files:
		syncByFile(file )
